
// 将对象解析成URL参数
export function urlencode(obj) {
	const params = []
	Object.keys(obj).forEach((key) => {
		let value = obj[key]
		// 如果值为undefined我们将其置空
		if (typeof value === 'undefined') {
			value = ''
		}
		params.push([key, encodeURIComponent(value)].join('='))
	})
	return params.join('&')
};
// 提示框 .then 函数是确定 catch 函数是取消
export function showmodel(params){
	params.title = params.title ||"提示";
	params.content = params.content || "是否继续";
	return new Promise((resolve,reject)=>{
		uni.showModal({
			...params,
			success(res) {
				if(res.confirm){
					resolve(true)
				}else{
					reject("")
				}
			}
		});
	});
};
// 设置头部导航栏的样式
export function setTopbar(params){
	uni.setNavigationBarColor({
		title:params.title || "萌卡",
		frontColor:params.frontColor || '#ffffff',
		backgroundColor:params.backgroundColor || '#F49e37',
		animation: params.animation || {
			duration: 400,
			timingFunc: 'easeIn'
		}
	});
};
export function routerLink(params){
	if(params.url){
		uni.navigateTo({
			url: params.url,
			animationType: params.animationType || "pop-in",
    		animationDuration: params.animationDuration || 200,
		});
	}
};
export function getElSize(id) { //得到元素的size
    return new Promise((res, rej) => {
          uni.createSelectorQuery().select('#' + id).fields({
               size: true,
               scrollOffset: true
           }, (data) => {
                  res(data);
           }).exec();
    });
};
export function throwErr(code, response){
	code = Number(code)
	let message = '请求错误'
	switch (code) {
		case 400:
			message = '请求错误'
			break
		case 401:
			message = '未授权，请登录'
			break
		case 403:
			message = '拒绝访问'
			break
		case 404:
			message = `请求地址出错: ${response.config.url}`
			break
		case 408:
			message = '请求超时'
			break
		case 500:
			message = '服务器内部错误'
			break
		case 501:
			message = '服务未实现'
			break
		case 502:
			message = '网关错误'
			break
		case 503:
			message = '服务不可用'
			break
		case 504:
			message = '网关超时'
			break
		case 505:
			message = 'HTTP版本不受支持'
			break
		default:
			message = false;
			break;
	}
	return message;
}